package net.area80.session
{
	import flash.events.TimerEvent;
	import flash.utils.clearInterval;
	import flash.utils.setInterval;
	
	import net.area80.utils.SmartLoader;
	
	import org.osflash.signals.Signal;

	public class SessionRefresh
	{
		public static var proxy:SessionRefresh;
		private var timeInterval:uint;
		private var intervalID:uint;
		private var url:String;
		public var SIGNAL_ONSESSION:Signal = new Signal(Boolean);
		
		public function SessionRefresh(timeinterval:uint,url:String)
		{
			if(SessionRefresh.proxy) throw new Error("session has created");
			SessionRefresh.proxy = this;
			timeInterval = timeinterval;
			this.url = url;
			start();
		}
		public function start ():void {
			if(!intervalID) {
				intervalID = setInterval(onTimeTick,timeInterval);
			} 
		}
		public function stop ():void {
			if(intervalID) clearInterval(intervalID);
		}
		private function onTimeTick ():void {
			var sm:SmartLoader = new SmartLoader();
			sm.SIGNAL_COMPLETE.addOnce(onSession);
			sm.load(url);
		}
		private function onSession (d:*):void {
			SIGNAL_ONSESSION.dispatch((String(d)=="1"));
		}
	}
}